package com.cykj.controller;


import com.cykj.dto.ResponseDTO;
import com.cykj.service.ICyGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/good")
public class CyGoodController {

    @Autowired
    private ICyGoodService cyGoodService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/buyGood")
    public ResponseDTO buyGood() {
        // 减库存
        redisTemplate.opsForValue().decrement("stock:" + 1);
        // 获取当前库存
        Integer stock = (Integer) redisTemplate.opsForValue().get("stock:" + 1);
        // 返回结果
        return ResponseDTO.success("购买成功","剩余库存："+ stock);
    }

    @RequestMapping("/updateStock")
    public ResponseDTO updateGoodStock(Integer goodId, Integer count){
        System.err.println(goodId+" -- "+count);
       return  cyGoodService.updateGoodStock(goodId, count);
    }

}
